package org.lanplay.server.pages;

import java.util.List;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.form.OnChangeAjaxBehavior;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.RequiredTextField;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.PropertyListView;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.LoadableDetachableModel;
import org.lanplay.server.dao.GameDao;
import org.lanplay.server.entitymodel.EntityModelFactory;
import org.lanplay.server.model.Game;

import com.google.inject.Inject;

public class SimpleGamesListPage extends TransactionAwarePage {
	private static final long serialVersionUID = 1L;

	@Inject
	private GameDao dao;

	@Inject
	private EntityModelFactory modelFactory;

	public SimpleGamesListPage() {
		add(new BookmarkablePageLink<WebPage>("linkToMain", WelcomePage.class));
		final GameDetailsForm gameForm = new GameDetailsForm("gameForm");
		add(gameForm);
		final PropertyListView<Game> gamesListView = new PropertyListView<Game>(
				"games", new LoadableDetachableModel<List<Game>>() {
					private static final long serialVersionUID = 1L;

					@Override
					protected List<Game> load() {
						return dao.getGamesList();
					}
				}) {
			private static final long serialVersionUID = 1L;

			@Override
			protected void populateItem(ListItem<Game> item) {
				item.add(new Label("name"));
				item.add(new Link<Game>("edit", item.getModel()) {
					private static final long serialVersionUID = 1L;

					@Override
					public void onClick() {
						gameForm.setModel(new CompoundPropertyModel<Game>(
								modelFactory.create(getModelObject())));
					}
				});
			}
		};
		add(gamesListView);
	}

	public class GameDetailsForm extends Form<Game> {
		private static final long serialVersionUID = 1L;

		public GameDetailsForm(String id) {
			super(id, new CompoundPropertyModel<Game>(
					modelFactory.create(new Game())));

			final FeedbackPanel feedbackPanel = new FeedbackPanel("feedback");
			feedbackPanel.setOutputMarkupId(true);
			add(feedbackPanel);
			final TextField<String> tfName = new RequiredTextField<String>(
					"name");
			tfName.add(new OnChangeAjaxBehavior() {
				private static final long serialVersionUID = 1L;

				@Override
				protected void onUpdate(AjaxRequestTarget target) {
					target.add(feedbackPanel);
				}

				@Override
				protected void onError(AjaxRequestTarget target,
						RuntimeException e) {
					target.add(feedbackPanel);
				}
			});
			add(tfName);
		}

		@Override
		protected void onSubmit() {
			Game game = getModelObject();
			if (dao.save(game)) {
				setModel(new CompoundPropertyModel<Game>(
						modelFactory.create(new Game())));
			} else {
				error("Couldn't save game");
			}
		}
	}
}
